home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 20 / CU Amiga Magazine's Super CD-ROM 20 (1998)(EMAP Images)(GB)[!][issue 1998-03].iso / CDsupport / ClassAct / Examples / ListBrowser / ListBrowserExample.c < prev    next >
C/C++ Source or Header  |  1997-07-09  |  15KB  |  543 lines

  1.  
  2. /**
  3.  **  ListBrowserTest.c -- List Browser class test.
  4.  **
  5.  **  This is a simple example testing some of the capabilities of the
  6.  **  ListBrowser gadget class.
  7.  **
  8.  **  This code opens a simple window and then a ListBrowser gadget which is
  9.  **  subsequently attached to the window's gadget list.  Everytime the user
  10.  **  clicks on the close gadget, this code changes some of the attributes
  11.  **  of the ListBrowser gadget to demonstrate different ways it can be used,
  12.  **  including one demonstration which creates two images using the Label
  13.  **  class and shows them in the ListBrowser.
  14.  **
  15.  **/
  16.  
  17.  
  18. #include <exec/types.h>
  19. #include <exec/memory.h>
  20. #include <intuition/intuition.h>
  21. #include <intuition/gadgetclass.h>
  22. #include <intuition/imageclass.h>
  23. #include <intuition/icclass.h>
  24. #include <utility/tagitem.h>
  25. #include <proto/dos.h>
  26. #include <proto/exec.h>
  27. #include <proto/intuition.h>
  28. #include <gadgets/listbrowser.h>
  29. #include <images/label.h>
  30. #include <proto/listbrowser.h>
  31. #include <proto/label.h>
  32. #include <string.h>
  33. #include <stdio.h>
  34.  
  35. /* Soon to be moved to gadgets/listbrowser.h
  36.  */
  37. #define    LBNCA_Editable        (LBNA_Dummy+13)
  38. #define    LBNCA_MaxChars        (LBNA_Dummy+14)
  39. #define    LBNCA_CopyText        (LBNA_Dummy+15)
  40. #define    LISTBROWSER_Editable            (LISTBROWSER_Dummy+33)
  41. #define    LISTBROWSER_Position            (LISTBROWSER_Dummy+34)
  42. #define    LISTBROWSER_EditNode            (LISTBROWSER_Dummy+35)
  43. #define    LISTBROWSER_EditColumn            (LISTBROWSER_Dummy+36)
  44. #define LBP_LINEUP 1
  45. #define LBP_LINEDOWN 2
  46. #define LBP_PAGEUP 3
  47. #define LBP_PAGEDOWN 4
  48. #define LBP_TOP 5
  49. #define LBP_BOTTOM 6
  50.  
  51. #define RAWKEY_CURSORUP 76
  52. #define RAWKEY_CURSORDOWN 77
  53. #define QUALIFIER_SHIFT 0x03
  54. #define QUALIFIER_ALT 0x30
  55. #define QUALIFIER_CTRL 0x08
  56.  
  57.  
  58. /* Function prototypes.
  59.  */
  60. BOOL make_list(struct List *, UBYTE **, LONG *);
  61. VOID free_list(struct List *);
  62. VOID wait_for_close(struct Window *, struct Gadget *);
  63. struct ClassLibrary * OpenClass(STRPTR, ULONG);
  64.  
  65. /* Global variables.
  66.  */
  67. struct ClassLibrary *ListBrowserBase;
  68. struct ClassLibrary *LabelBase;
  69.  
  70.  
  71. UBYTE *col1[] =
  72. {
  73.     "This is a", "test of the", "ListBrowser", "gadget class.",
  74.     "This is like", "a souped-up", "listview", "gadget.  It", "has many",
  75.     "cool new", "features", "though like", "multiple", "columns,",
  76.     "horizontal", "scrolling,", "images in", "nodes,", "columns titles",
  77.     "and much much", "more!",
  78.     "This is a", "test of the", "ListBrowser", "gadget class.",
  79.     "This is like", "a souped-up", "listview", "gadget.  It", "has many",
  80.     "cool new", "features", "though like", "multiple", "columns,",
  81.     "horizontal", "scrolling,", "images in", "nodes,", "columns titles",
  82.     "and much much", "more!",
  83.     "This is a", "test of the", "ListBrowser", "gadget class.",
  84.     "This is like", "a souped-up", "listview", "gadget.  It", "has many",
  85.     "cool new", "features", "though like", "multiple", "columns,",
  86.     "horizontal", "scrolling,", "images in", "nodes,", "columns titles",
  87.     "and much much", "more!", NULL
  88. };
  89.  
  90. LONG col2[] =
  91. {
  92.     1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  93.     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
  94.     27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  95.     39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
  96.     51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
  97.     63
  98. };
  99.  
  100. struct ColumnInfo ci[] =
  101. {
  102.     { 80, "Col 1", 0 },
  103.     { 60, "Col 2", 0 },
  104.     { 60, "Col 3", 0 },
  105.     { -1, (STRPTR)~0, -1 }
  106. };
  107.  
  108. struct ColumnInfo fancy_ci[] =
  109. {
  110.     { 100, NULL, 0 },
  111.     { -1, (STRPTR)~0, -1 }
  112. };
  113.  
  114. /* Some fonts for our fancy list.
  115.  */
  116. struct TextAttr helvetica24b = { (STRPTR)"helvetica.font", 24, FSF_BOLD, FPF_DISKFONT };
  117. struct TextAttr times18i = { (STRPTR)"times.font", 18, FSF_ITALIC, FPF_DISKFONT };
  118. struct TextAttr times18 = { (STRPTR)"times.font", 18, 0, FPF_DISKFONT };
  119.  
  120.  
  121. /* This is the start of our programme.
  122.  */
  123. main()
  124. {
  125.     struct Screen *screen = NULL;
  126.  
  127.     /* We'll just open up on the default public screen, and use its screen font.
  128.      */
  129.     if (screen = LockPubScreen(NULL))
  130.     {
  131.         struct Window *win = NULL;
  132.  
  133.         /* Open the window, note how we size the window to perfectly fit
  134.          * all the gadgets.
  135.          */
  136.         if (win = OpenWindowTags(NULL,
  137.             WA_Left, 0,
  138.             WA_Top, screen->Font->ta_YSize + 3,
  139.             WA_Width, 300,
  140.             WA_Height, 160,
  141.             WA_CustomScreen, screen,
  142.             WA_IDCMP, IDCMP_GADGETUP | IDCMP_MOUSEMOVE | IDCMP_RAWKEY |
  143.                         IDCMP_CLOSEWINDOW | IDCMP_GADGETDOWN,
  144.             WA_Flags, WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET |
  145.                         WFLG_SIZEGADGET | WFLG_ACTIVATE | WFLG_SMART_REFRESH,
  146.             WA_Title, "ListBrowser Class Demo",
  147.             WA_MinWidth, 50,
  148.             WA_MinHeight, 50,
  149.             WA_MaxWidth, -1,
  150.             WA_MaxHeight, -1,
  151.             TAG_DONE))
  152.         {
  153.             /* Create the private BOOPSI image class.
  154.              */
  155.             PutStr("Creating ListBrowser class\n");
  156.             if (ListBrowserBase = OpenClass("gadgets/listbrowser.gadget", 0))
  157.             {
  158.                 struct Gadget *listbrowser_gad;
  159.                 struct List label_list;
  160.  
  161.                 make_list(&label_list, col1, col2);
  162.  
  163.                 /* Create a listbrowser gadget.
  164.                  */
  165.                 PutStr("Creating ListBrowser object\n");
  166.                 if (listbrowser_gad = (struct Gadget *)NewObject(LISTBROWSER_GetClass(), NULL,
  167.                                                         GA_ID, 1,
  168.                                                         GA_Top, win->BorderTop + 5,
  169.                                                         GA_Left, 10,
  170.                                                         GA_RelWidth, -34,
  171.                                                         GA_RelHeight, -(win->BorderTop + win->BorderBottom + 10),
  172.                                                         GA_RelVerify, TRUE,
  173.                                                         LISTBROWSER_Labels, (ULONG)&label_list,
  174.                                                         LISTBROWSER_ColumnInfo, (ULONG)&ci,
  175.                                                         LISTBROWSER_ColumnTitles, TRUE,
  176.                                                         LISTBROWSER_MultiSelect, FALSE,
  177.                                                         LISTBROWSER_Separators, TRUE,
  178.                                                         LISTBROWSER_ShowSelected, FALSE,
  179.                                                         LISTBROWSER_Editable, TRUE,
  180.                                                         TAG_END))
  181.                 {
  182.                     /* Adding gadgets.
  183.                      */
  184.                     PutStr("Adding gadget\n");
  185.                     AddGList(win, listbrowser_gad, -1, -1, NULL);
  186.                     PutStr("Refreshing gadget\n");
  187.                     RefreshGList(listbrowser_gad, win, NULL, -1);
  188.  
  189.                     /* Wait for close gadget click to continue.
  190.                      */
  191.                     SetWindowTitles(win, "<- Click here to continue", (UBYTE *)~0);
  192.                     wait_for_close(win, listbrowser_gad);
  193.  
  194.                     /* Make Visible
  195.                      */
  196.                     SetWindowTitles(win, "Make Visible 10", (UBYTE *)~0);
  197.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  198.                         LISTBROWSER_MakeVisible, 10,
  199.                         LISTBROWSER_EditNode, 8,
  200.                         LISTBROWSER_EditColumn, 1,
  201.                         TAG_DONE);
  202.                     ActivateGadget(listbrowser_gad, win, NULL);
  203.                     wait_for_close(win, listbrowser_gad);
  204.  
  205.                     /* Show selected
  206.                      */
  207.                     SetWindowTitles(win, "Show selected, Auto-Fit", (UBYTE *)~0);
  208.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  209.                         LISTBROWSER_ShowSelected, TRUE,
  210.                         LISTBROWSER_AutoFit, TRUE,
  211.                         LISTBROWSER_HorizontalProp, TRUE,
  212.                         TAG_DONE);
  213.                     wait_for_close(win, listbrowser_gad);
  214.  
  215.                     /* Multi-select
  216.                      */
  217.                     SetWindowTitles(win, "Multi-select, Virtual Width of 500", (UBYTE *)~0);
  218.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  219.                         LISTBROWSER_MultiSelect, TRUE,
  220.                         LISTBROWSER_VirtualWidth, 500,
  221.                         LISTBROWSER_AutoFit, FALSE,
  222.                         TAG_DONE);
  223.                     wait_for_close(win, listbrowser_gad);
  224.  
  225.                     /* Detach the list.
  226.                      */
  227.                     SetWindowTitles(win, "Detached list", (UBYTE *)~0);
  228.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  229.                         LISTBROWSER_MultiSelect, FALSE,
  230.                         LISTBROWSER_Labels, ~0,
  231.                         TAG_DONE);
  232.                     wait_for_close(win, listbrowser_gad);
  233.  
  234.                     /* No separators, no title, 1 column.
  235.                      */
  236.                     SetWindowTitles(win, "No separators, no title, 1 column.", (UBYTE *)~0);
  237.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  238.                         LISTBROWSER_Labels, (ULONG)&label_list,
  239.                         LISTBROWSER_ColumnInfo, (ULONG)&fancy_ci,
  240.                         LISTBROWSER_Separators, FALSE,
  241.                         LISTBROWSER_ColumnTitles, FALSE,
  242.                         LISTBROWSER_AutoFit, TRUE,
  243.                         LISTBROWSER_VirtualWidth, 0,
  244.                         TAG_DONE);
  245.                     wait_for_close(win, listbrowser_gad);
  246.  
  247.                     /* Fancy list.
  248.                      */
  249.                     PutStr("Creating Label class\n");
  250.                     if (LabelBase = OpenClass("images/label.image", 0))
  251.                     {
  252.                         struct List fancy_list;
  253.                         struct Image *image1, *image2;
  254.                         struct Node *node1, *node2;
  255.  
  256.                         NewList(&fancy_list);
  257.  
  258.                         PutStr("Creating Label object\n");
  259.                         if (image1 = (struct Image *)NewObject(LABEL_GetClass(), NULL,
  260.                                                         IA_FGPen, 1,
  261.                                                         IA_BGPen, 2,
  262.                                                         IA_Font, (ULONG)&helvetica24b,
  263.                                                         LABEL_Text, (ULONG)"C",
  264.                                                         IA_Font, (ULONG)×18i,
  265.                                                         LABEL_Text, (ULONG)"lass ",
  266.                                                         IA_Font, (ULONG)&helvetica24b,
  267.                                                         LABEL_Text, (ULONG)"A",
  268.                                                         IA_Font, (ULONG)×18i,
  269.                                                         LABEL_Text, (ULONG)"ct",
  270.                                                         TAG_END))
  271.                         {
  272.                             PutStr("Creating Label object\n");
  273.                             if (image2 = (struct Image *)NewObject(LABEL_GetClass(), NULL,
  274.                                                             IA_FGPen, 2,
  275.                                                             IA_BGPen, 0,
  276.                                                             IA_Font, (ULONG)×18,
  277.                                                             LABEL_Text, (ULONG)"By Phantom Development",
  278.                                                             TAG_END))
  279.                             {
  280.                                 if (node1 = AllocListBrowserNode(1,
  281.                                                 LBNA_Column, 0,
  282.                                                     LBNCA_Image, (ULONG)image1,
  283.                                                     LBNCA_Justification, LCJ_CENTRE,
  284.                                                 TAG_DONE))
  285.                                 {
  286.                                     AddTail(&fancy_list, node1);
  287.  
  288.                                     if (node2 = AllocListBrowserNode(1,
  289.                                                     LBNA_Column, 0,
  290.                                                         LBNCA_Image, (ULONG)image2,
  291.                                                         LBNCA_Justification, LCJ_CENTRE,
  292.                                                     TAG_DONE))
  293.                                     {
  294.                                         AddTail(&fancy_list, node2);
  295.  
  296.                                         /* Set listbrowser.
  297.                                          */
  298.                                         SetWindowTitles(win, "Fancy", (UBYTE *)~0);
  299.                                         SetGadgetAttrs(listbrowser_gad, win, NULL,
  300.                                             LISTBROWSER_ColumnInfo, &fancy_ci,
  301.                                             LISTBROWSER_Labels, (ULONG)&fancy_list,
  302.                                             LISTBROWSER_AutoFit, TRUE,
  303.                                             TAG_DONE);
  304.  
  305.                                         wait_for_close(win, listbrowser_gad);
  306.  
  307.                                         SetGadgetAttrs(listbrowser_gad, win, NULL,
  308.                                             LISTBROWSER_Labels, ~0,
  309.                                             TAG_DONE);
  310.  
  311.                                         FreeListBrowserNode(node2);
  312.                                     }
  313.                                     FreeListBrowserNode(node1);
  314.                                 }
  315.                                 DisposeObject(image2);
  316.                             }
  317.                             DisposeObject(image1);
  318.                         }
  319.  
  320.                         PutStr("Closing Label class\n");
  321.                         CloseLibrary((struct Library *)LabelBase);
  322.                     }
  323.                     else
  324.                         PutStr("ERROR: Couldn't create Label class\n");
  325.  
  326.                     /* Read-only
  327.                      */
  328.                     SetWindowTitles(win, "Read-only", (UBYTE *)~0);
  329.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  330.                         LISTBROWSER_ColumnInfo, (ULONG)&ci,
  331.                         LISTBROWSER_Labels, (ULONG)&label_list,
  332.                         LISTBROWSER_AutoFit, TRUE,
  333.                         LISTBROWSER_Selected, -1,
  334.                         GA_ReadOnly, TRUE,
  335.                         TAG_DONE);
  336.                     wait_for_close(win, listbrowser_gad);
  337.  
  338.                     /* Disabled.
  339.                      */
  340.                     SetWindowTitles(win, "Disabled", (UBYTE *)~0);
  341.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  342.                         GA_Disabled, TRUE,
  343.                         GA_ReadOnly, FALSE,
  344.                         TAG_DONE);
  345.                     wait_for_close(win, listbrowser_gad);
  346.  
  347.                     /* No scrollbars, borderless.  (Note that it isn't
  348.                      * correct to hard-code the window border size like
  349.                      * this, I was just being lazy :)
  350.                      */
  351.                     SetWindowTitles(win, "No scrollbars, borderless", (UBYTE *)~0);
  352.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  353.                         GA_Disabled, FALSE,
  354.                         GA_Top, win->BorderTop,
  355.                         GA_Left, 4,
  356.                         GA_RelWidth, -22,
  357.                         GA_RelHeight, -(win->BorderTop + win->BorderBottom),
  358.                         LISTBROWSER_Borderless, TRUE,
  359.                         LISTBROWSER_HorizontalProp, FALSE,
  360.                         LISTBROWSER_VerticalProp, FALSE,
  361.                         TAG_DONE);
  362.                     wait_for_close(win, listbrowser_gad);
  363.  
  364.                     RemoveGList(win, listbrowser_gad, -1);
  365.                     DisposeObject(listbrowser_gad);
  366.                 }
  367.                 else
  368.                     PutStr("ERROR: Couldn't create ListBrowser gadgetn");
  369.  
  370.                 free_list(&label_list);
  371.  
  372.                 PutStr("Freeing ListBrowser class\n");
  373.                 CloseLibrary((struct Library *)ListBrowserBase);
  374.             }
  375.             else
  376.                 PutStr("ERROR: Couldn't create ListBrowser class\n");
  377.  
  378.             CloseWindow(win);
  379.         }
  380.         else
  381.             PutStr("ERROR: Couldn't open window\n");
  382.  
  383.         UnlockPubScreen(0, screen);
  384.     }
  385.     else
  386.         PutStr("ERROR: Couldn't lock public screen\n");
  387. }
  388.  
  389.  
  390. /* Function to make a List of ListBrowserNodes from a couple of arrays.
  391.  * Just to demonstrate things, we make make three columns, 2 with text
  392.  * (the same text) and the third with numbers.
  393.  */
  394. BOOL make_list(struct List *list, UBYTE **labels1, LONG *labels2)
  395. {
  396.     struct Node *node;
  397.     WORD i = 0;
  398.  
  399.     NewList(list);
  400.  
  401.     while (*labels1)
  402.     {
  403.         if (node = AllocListBrowserNode(3,
  404.                         LBNA_Column, 0,
  405.                             LBNCA_CopyText, TRUE,
  406.                             LBNCA_Text, *labels1,
  407.                             LBNCA_MaxChars, 40,
  408.                             LBNCA_Editable, TRUE,
  409.                         LBNA_Column, 1,
  410.                             LBNCA_CopyText, TRUE,
  411.                             LBNCA_Text, *labels1,
  412.                             LBNCA_MaxChars, 40,
  413.                             LBNCA_Editable, TRUE,
  414.                         LBNA_Column, 2,
  415.                             LBNCA_Integer, &labels2[i],
  416.                             LBNCA_Justification, LCJ_RIGHT,
  417.                         TAG_DONE))
  418.         {
  419.             AddTail(list, node);
  420.         }
  421.         else
  422.             break;
  423.  
  424.         labels1++;
  425.         i++;
  426.     }
  427.     return(TRUE);
  428. }
  429.  
  430.  
  431. /* Function to free an Exec List of ListBrowser nodes.
  432.  */
  433. VOID free_list(struct List *list)
  434. {
  435.     struct Node *node, *nextnode;
  436.  
  437.     node = list->lh_Head;
  438.     while (nextnode = node->ln_Succ)
  439.     {
  440.         FreeListBrowserNode(node);
  441.         node = nextnode;
  442.     }
  443.     NewList(list);
  444. }
  445.  
  446.  
  447. /* Function to open a BOOPSI class library.
  448.  */
  449. struct ClassLibrary * OpenClass(STRPTR name, ULONG version)
  450. {
  451.     struct Library *retval;
  452.     UBYTE buffer[256];
  453.  
  454.     if ((retval = OpenLibrary(name, version)) == NULL)
  455.     {
  456.         sprintf (buffer, ":classes/%s", name);
  457.         if ((retval = OpenLibrary(buffer, version)) == NULL)
  458.         {
  459.             sprintf(buffer, "classes/%s", name);
  460.             retval = OpenLibrary(buffer, version);
  461.         }
  462.     }
  463.     return((struct ClassLibrary *)retval);
  464. }
  465.  
  466. /* Wait for the window close gadget to be pressed.
  467.  */
  468. VOID wait_for_close(struct Window *win, struct Gadget *listbrowser_gad)
  469. {
  470.     BOOL ok = TRUE;
  471.  
  472.     /* Just wait around until the close gadget is pressed.
  473.      */
  474.     while (ok)
  475.     {
  476.         struct Gadget *gadget;
  477.         struct IntuiMessage *imsg;
  478.             
  479.         WaitPort(win->UserPort);
  480.         while (imsg = (struct IntuiMessage *)GetMsg(win->UserPort))
  481.         {
  482.             switch (imsg->Class)
  483.             {
  484.                 case IDCMP_CLOSEWINDOW:
  485.                     ok = FALSE;
  486.                     break;
  487.  
  488.                 case IDCMP_RAWKEY:
  489.                     if (!(imsg->Code & IECODE_UP_PREFIX))
  490.                     {
  491.                         switch (imsg->Code)
  492.                         {
  493.                             case RAWKEY_CURSORUP:
  494.                                 if (imsg->Qualifier & QUALIFIER_CTRL)
  495.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  496.                                         LISTBROWSER_Position, LBP_TOP,
  497.                                         TAG_DONE);
  498.                                 if (imsg->Qualifier & QUALIFIER_SHIFT)
  499.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  500.                                         LISTBROWSER_Position, LBP_PAGEUP,
  501.                                         TAG_DONE);
  502.                                 else
  503.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  504.                                         LISTBROWSER_Position, LBP_LINEUP,
  505.                                         TAG_DONE);
  506.                                 break;
  507.  
  508.                             case RAWKEY_CURSORDOWN:
  509.                                 if (imsg->Qualifier & QUALIFIER_CTRL)
  510.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  511.                                         LISTBROWSER_Position, LBP_BOTTOM,
  512.                                         TAG_DONE);
  513.                                 if (imsg->Qualifier & QUALIFIER_SHIFT)
  514.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  515.                                         LISTBROWSER_Position, LBP_PAGEDOWN,
  516.                                         TAG_DONE);
  517.                                 else
  518.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  519.                                         LISTBROWSER_Position, LBP_LINEDOWN,
  520.                                         TAG_DONE);
  521.                                 break;
  522.  
  523.                             default:
  524.                                 break;
  525.                         }
  526.                     }
  527.                     break;
  528.  
  529.                 case IDCMP_GADGETUP:
  530.                     gadget = (struct Gadget *)imsg->IAddress;
  531.                     Printf("Gadget: %ld  Code: %ld\n",
  532.                         (LONG)gadget->GadgetID, (LONG)imsg->Code);
  533.  
  534.                     break;
  535.         
  536.                 default:
  537.                     break;
  538.             }
  539.             ReplyMsg((struct Message *)imsg);
  540.         }
  541.     }
  542. }
  543.